package com.orchidea.uml.model.document
{
import com.orchidea.uml.model.*;

import com.orchidea.uml.orchidea_internal;

import flash.events.Event;
import flash.events.EventDispatcher;
    import mx.collections.ArrayCollection;
import mx.collections.ICollectionView;

/**
	 * Abstract superclass of all document types.
	 */
    public class Document extends NamedElementImpl
    {

        private var _elements : ArrayCollection = new ArrayCollection();
        [Bindable(event="elementsChanged")]
        public function get elements():ICollectionView { return _elements; }

        private var _connections : ArrayCollection = new ArrayCollection();
        [Bindable(event="connectionsChanged")]
        public function get connections():ICollectionView { return _connections; }


        private var _type  : DocumentType;
        public function get type( ) : DocumentType { return _type; }

        private var _title  : String;
        [Bindable(event="change")]
		public function get title( ) : String { return _title;}
        

        public function Document( type : DocumentType )
        {
            _type = type;
        }

        orchidea_internal function setTitle( title : String ) : void { setTitleImpl(title); }

		/**
		 * This is the actual implementation of setTitle. Because there are problems with overriding methods
         * in custom namespaces this method is needed to let subclasses override the setting of the title.
		 */
		protected function setTitleImpl( title : String ) : void
        {
			_title = title;
			dispatchModelChanged();
		}

        public function add(item:DocumentNode):void
        {
            _elements.addItem(item);
            dispatchEvent(new Event("elementsChanged"));
            dispatchModelChanged();
        }

        public function remove(item:DocumentNode):void
        {

            dispatchEvent(new Event("elementsChanged"));
            dispatchModelChanged();
        }

        public function link(item1:DocumentNode, item2:DocumentNode, data:Object=null):void
        {
            var edge:DocumentEdge = new DocumentEdge(item1, item2);
            _connections.addItem(edge);

            dispatchEvent(new Event("connectionsChanged"));
            dispatchModelChanged();
        }

        public function unlink(item1:IEntity, item2:IEntity):void
        {

            dispatchEvent(new Event("connectionsChanged"));
            dispatchModelChanged();
        }



        private function dispatchModelChanged():void
        {
            dispatchEvent(new Event(Event.CHANGE));
        }
    }
}